package com.zfy.cmsservice.controller;

import com.zfy.cmsservice.entity.Courses;
import com.zfy.cmsservice.service.CoursesService;
import com.zfy.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-12-03
 */
@RestController
@RequestMapping("/cmsservice/courses")
@Api(description = "课程管理-课程信息维护")
public class CoursesController {
    @Autowired CoursesService coursesService;

    // 分页查询课程 根据名称检索
    @ApiOperation(value = "分页查询课程")
    @PostMapping("pageCourses/{page}/{size}")
    public R pageCourses(@PathVariable long page, @PathVariable long size,
                         @RequestParam(required = false) String name) {
        return coursesService.pageCourses(page,size,name);
    }

    // 添加课程
    @ApiOperation(value = "添加课程")
    @PostMapping("addCourses")
    public R addCourses(@RequestBody Courses courses) {
        boolean flag = coursesService.save(courses);
        if(flag) {
            return R.ok().message("添加成功");
        }
        return R.error().message("添加失败");
    }

//   修改课程信息
    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourses")
    public R updateCourses(@RequestBody Courses courses) {
        if (StringUtils.isEmpty(courses)) {
            // 传参错误异常
            return R.httpError();
        }
        boolean update = coursesService.updateById(courses);
        if(update) {
            return R.ok().message("修改成功");
        }
        return R.error().message("修改失败");
    }

    // 逻辑删除课程信息
    @ApiOperation(value = "逻辑删除课程信息")
    @PostMapping("removeCourses/{id}")
    public R removeCourses(@PathVariable String id){
        boolean remove = coursesService.removeById(id);
        if(remove) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    //根据id查询课程
    @ApiOperation(value = "根据id查询班級")
    @GetMapping("getCourse/{id}")
    public R getClassById(@PathVariable String id) {
        Courses byId = coursesService.getById(id);
        return R.ok().data("item", byId);
    }

//    获取所有课程
    @ApiOperation(value = "获取所有课程")
    @GetMapping("findAllTeacher")
    public R findAllTeacher() {
        List<Courses> content = coursesService.findAllTeacher();
        return R.ok().data("content", content);
    }

//    查询所有选修课
    @ApiOperation(value = "查询所有选修课")
    @GetMapping("getElective")
    public R getElective() {
        List<Courses> content = coursesService.getElective();
        return R.ok().data("content", content);
    }

}

